<template>
  <div class="box" shadow="never">
    <el-form ref="form" :model="list" label-width="120px">
      <el-form-item label-width="100px" label="账户:">
        <el-input v-model="list.account" style="width: 400px"> </el-input>
      </el-form-item>

      <el-form-item label-width="100px" label="电话:">
        <el-input v-model="list.phone" style="width: 400px"> </el-input>
      </el-form-item>

      <el-form-item label-width="100px" label="邮箱:">
        <el-input v-model="list.email" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="标签:">
        <el-input v-model="list.label" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="用户名:">
        <el-input v-model="list.username" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="密码:">
        <el-input v-model="list.password" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="昵称:">
        <el-input v-model="list.nick_name" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="真实姓名:">
        <el-input v-model="list.real_name" style="width: 400px" />
      </el-form-item>

      <!-- <el-form-item label-width="100px" label="封面:">
                    <el-input v-model="list.head_picture" style="width: 400px;" />
              </el-form-item> -->

      <el-form-item label-width="100px" label="人物头像:">
        <!-- <el-input v-model="list.profile" style="width: 400px;" /> -->
        <el-upload
          class="avatar-uploader"
          action="http://42.194.159.131:8080/pic/imgUpload?path=headpic"
          :headers="headers"
          :show-file-list="true"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="list.head_picture"
            :src="list.head_picture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div slot="tip" class="el-upload__tip">
            只能上传图片为jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label-width="100px" label="状态:">
        <!-- <el-input v-model="list.status" style="width: 400px;" /> -->
        <el-radio-group v-model="list.status">
          <el-radio label="0">用户状态打开</el-radio>
          <el-radio label="1">用户状态关闭</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import controll from "@/api/usercontroll";
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      list: {
        username: "",
        head_picture:""
      },
       headers: {
	        'Authorization':getToken()
	      }
    };
  },
  methods: {
    onSubmit() {
      var vm = this;
      controll
        .aduser(vm.list)
        .then(function (resp) {
          vm.$message({
            message: "添加成功!",
            type: "success",
          });
          vm.$router.go(-1);
        })
        .catch(function (error) {
          console.log("--------------------asdsa");
          vm.$message.error("添加失败");
        });
    },
      //上传图片相关回调
    handleAvatarSuccess(res, file) {
        this.list.head_picture = res.data.picUrl;
        console.log("上传图片成功",this.list.head_picture)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
         console.log("beforeAvatarUpload",file)
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;

      }
  },
};
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}

.box {
  margin-left: 200px;
  height: 1100px;
  width: 700px;
  border: solid rgb(187, 186, 187);
}
 .avatar-uploader .el-upload {
    border: 3px dashed #decfcf;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
